<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/11 0011
 * Time: 10:26
 */

namespace app\admin\controller;


use app\common\model\Reason;
use think\Db;
use think\Exception;

class Report extends Base
{
      public function  index(){
          $map = [];
          if(!isEmpty(input('status'))){
              $map['r.status']=trim(input('get.status'));
          }
          $map['r.type'] = 1;
          $model = new  Reason();
          $rows=$model->getRoomList($map);

          $this->assign([
              'type' => $map['r.type'],
              'rows'=>$rows,
              'pageHTML'=>$rows->render()
          ]);
          return view();
      }


    public function user()
    {
        $map = [];
        if(!isEmpty(input('status'))){
            $map['r.status']=trim(input('get.status'));
        }
        $map['r.type'] = 2;
        $model = new  Reason();
        $rows=$model->getUserList($map);
        $this->assign([
            'type' => $map['r.type'],
            'rows'=>$rows,
            'pageHTML'=>$rows->render()
        ]);
        return view('index');
    }


    public function delete(){
          $this->_del('reason');
    }

    public function  report_edit(){
        $model = new Reason();
        $param = input('param.');

        $data  = $model->detail($param['id']);

        $this->assign('data',$data);

       if(request()->isPost()){

           if ($data['status'] != 0){
               $this->error('该举报已处理');
           }
            Db::startTrans();
            try{
                $result = $this->_change('reason','status','status',true);
                if ($result){
                    if (input('status') == 1){
                        if ($data['type'] == 1){
                            $url = url('index');
                            $table = 'room';
                            //发送系统通知  强退房间内的所有人
                            \app\api\controller\Base::sendRoomMsg($data['id'],'exit');
                        }else{
                            $this->banedUser($param['id'],$param['end_time'],'您的账号因被其他用户举报,已被管理员');
                            $url = url('user');
                            $table = 'users';
                            cache('token_'.hashid($data['id']),null);
                        }
                        $this->_changeField($table,$data['id']);
                    }
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('操作成功',$url);

        }
        return view();
    }
}